package com.hornsun.manage.member.api;

import com.hornsun.util.StrUtil;
import com.hornsun.domain.dto.MemberWechatDTO;
import com.hornsun.manage.member.dto.MemberDTO;
import com.hornsun.manage.member.dto.StatisticsDTO;
import com.hornsun.repository.MemberWechatRepository;
import com.hornsun.repository.mapper.MemberWechatMapper;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 编译中... on 2018/5/17.
 */
@RestController
@RequestMapping("/manage/memberWechat")
public class MemberWechatApi {

    @Resource
    private MemberWechatMapper memberWechatMapper;

    @Resource
    private MemberWechatRepository memberWechatRepository;

    /**
     * 会员数据统计
     * @return
     */
    @GetMapping("/statistics")
    public ResponseEntity<StatisticsDTO> getStatistics(){
        StatisticsDTO statisticsDTO = new StatisticsDTO();
        statisticsDTO.setGender(memberWechatMapper.genderStatistics());
        statisticsDTO.setSystem(memberWechatMapper.terminalStatistics());
        statisticsDTO.setCity(memberWechatMapper.cityStatistics());
        return new ResponseEntity<StatisticsDTO>(statisticsDTO, HttpStatus.OK);
    }

    /**
     * 获得所有会员信息
     * @param page
     * @return
     */
    @GetMapping("/getMembers")
    public ResponseEntity<?> getMembers(@RequestParam("page") int page){
        MemberDTO memberDTO = new MemberDTO();
        memberDTO.setCount(memberWechatRepository.totalCount());
        memberDTO.setMemberList(memberWechatRepository.getMemberInfo(page));
        return new ResponseEntity<Object>(memberDTO, HttpStatus.OK);
    }

    /**
     * 模糊查询
     * @param content
     * @return
     */
    @GetMapping("/fuzzyQuery")
    public ResponseEntity<?> fuzzyQuery(@RequestParam("content")String content){
        List<MemberWechatDTO> memberWechatDTOList = new ArrayList<>();
        if (StrUtil.isPhoneNumber(content)){
             memberWechatDTOList= memberWechatMapper.getMemberInfoByMobile(content);
        }else {
            memberWechatDTOList=memberWechatMapper.getMemberInfoByNickname(content);
        }
        return new ResponseEntity<Object>(memberWechatDTOList,HttpStatus.OK);
    }


}
